package com.hspedu.homework;

public class Homework01 {
    public static void main(String[] args) {
        String s = "asdfg";
        System.out.println("====交换前====");
        System.out.println(s);
        // 反转 s 到 f
        try {
            s = reverse(s, 1, 4);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return;
        }
        System.out.println("====交换后====");
        System.out.println(s);
    }

    // 将字符串指定部分反转
    public static String reverse(String str, int start, int end) {
        // 对输入的参数做一个验证
        if (!(str != null && start >= 0 && end > start && end < str.length())) {
            throw new RuntimeException("参数不正确");
        }
        char[] chars = str.toCharArray();
        char temp = ' ';
        while (start < end) {
            temp = chars[start];
            chars[start] = chars[end];
            chars[end] = temp;
            start++;
            end--;
        }
        return new String(chars);
    }
}
